package com.example.fang.demodruid.concurrency5;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class MyTest3 {

    public static void main(String[] args){
        Callable<Integer> callable=()->{
            Thread.sleep(5000);
            System.out.println("pre execution");
            int randomNumber =new Random().nextInt(500);
            System.out.println("post execution");
            return randomNumber;
        };
        FutureTask<Integer> futureTask=new FutureTask(callable);
        new Thread(futureTask).start();
        System.out.println("thread has stareted");
        try {
            Thread.sleep(2000);
           System.out.println(futureTask.get(1, TimeUnit.MILLISECONDS));
        }catch (Exception e){
            System.out.println("执行异常");
           e.printStackTrace();
        }

    }
}
